<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { ContentDetailWrap } from '@/components/ContentDetailWrap'
import { watch, inject, reactive, Ref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { Row } from '@/api/logistics/vehicle'
const { required } = useValidator()
const emit = defineEmits(['close', 'get-list'])
const currentRow: Ref<Row> = inject('currentRow') as Ref<Row>
const { formRegister, formMethods } = useForm()

const { setValues } = formMethods

const schema = reactive<FormSchema[]>([
  {
    field: '',
    label: '基础信息',
    component: 'Divider',
    colProps: { span: 28 },
    componentProps: {
      borderStyle: 'none',
      contentPosition: 'left'
    }
  },
  {
    field: 'no',
    label: '车牌号',
    component: 'Input',
    formItemProps: {
      rules: [required(), { message: '请输入内容', trigger: 'blur' }]
    },
    componentProps: {
      disabled: true,
      placeholder: '请输入内容'
    }
  },
  {
    field: 'type',
    label: '车型',
    component: 'Select',
    formItemProps: {
      rules: [required()]
    },
    componentProps: {
      disabled: true,
      placeholder: '请选择',
      options: [
        {
          label: '小面',
          value: 1
        },
        {
          label: '中面',
          value: 2
        },
        {
          label: '微面',
          value: 3
        },
        {
          label: '小货',
          value: 4
        },
        {
          label: '中货',
          value: 5
        },
        {
          label: '大货',
          value: 6
        },
        {
          label: '冷藏车',
          value: 7
        },
        {
          label: '私家车',
          value: 8
        }
      ]
    }
  },
  {
    field: 'length',
    label: '车长',
    component: 'Select',
    componentProps: {
      disabled: true,
      placeholder: '请选择',
      options: [
        {
          label: '4米2',
          value: 1
        },
        {
          label: '5米2',
          value: 2
        },
        {
          label: '6米8',
          value: 3
        },
        {
          label: '7米6',
          value: 4
        },
        {
          label: '9米6',
          value: 5
        },
        {
          label: '13米',
          value: 6
        },
        {
          label: '其他',
          value: 7
        }
      ]
    }
  },
  {
    field: 'remark',
    label: '备注信息',
    component: 'Input',
    componentProps: {
      disabled: true,
      placeholder: '请输入'
    }
  },
  {
    field: 'color',
    label: '车辆颜色',
    component: 'Input',
    componentProps: {
      disabled: true,
      placeholder: '请输入'
    }
  },
  {
    field: 'passenger_num',
    label: '核定载客',
    component: 'Input',
    componentProps: {
      disabled: true,
      placeholder: '请输入'
    }
  },
  {
    field: 'volume',
    label: '核载体积（m³）',
    component: 'Input',
    componentProps: {
      disabled: true,
      placeholder: '请输入'
    }
  },
  {
    field: 'load_weight',
    label: '核载重量（kg）',
    component: 'Input',
    componentProps: {
      disabled: true,
      placeholder: '请输入'
    }
  },
  {
    field: 'brand',
    label: '车辆品牌',
    component: 'Input',
    componentProps: {
      disabled: true,
      placeholder: '请输入'
    }
  },
  {
    field: '',
    label: '',
    component: 'Divider',
    colProps: { span: 28 }
  },
  {
    field: 'img',
    label: '车辆图片',
    formItemProps: {
      slots: {
        default: (data) => {
          if (data.img && Array.isArray(data.img)) {
            return (
              <div>
                {data.img.map((imgUrl, index) => (
                  <img
                    src={imgUrl}
                    key={index}
                    style={{
                      width: '100px',
                      height: '100px',
                      objectFit: 'cover',
                      marginRight: '20px'
                    }}
                  />
                ))}
              </div>
            )
          } else {
            return null // 或者返回一些默认内容或占位符
          }
        }
      }
    }
  }
])

watch(
  () => currentRow.value,
  (currentRow) => {
    if (!currentRow) return
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)
</script>

<template>
  <ContentDetailWrap title="车辆详情">
    <Form @register="formRegister" :schema="schema" />
    <template #footer>
      <div class="w-full flex items-center justify-center">
        <BaseButton @click="emit('close')">返回</BaseButton>
      </div>
    </template>
  </ContentDetailWrap>
</template>
